// subPages/shopPage/order/order.js
const app = getApp();
var utils = require('../../../utils/util');
Page({
  /**
   * 页面的初始数据
   */
  data: {
      userInfo:app.globalData.userInfo,
      goodsInfo:'',
      buyNum:1,
      totalPrice:0,
      address:'',
      orderPrice:0
  },
    submitOrder(){
      var that = this;
      if(that.data.address==null){
          wx.showToast({
              title:'请添加收货地址！',
              icon:'none',
              duration:1000
          })
      }else{
          var product = [{
              shop_id:this.data.goodsInfo.shop_id,
              content_id:this.data.goodsInfo.id,
              num:this.data.buyNum
          }];
          product = JSON.stringify(product);
          var userid = app.globalData.userId;
          var orderPrice = this.data.orderPrice;
          var orderJf = this.data.totalPrice;
          var address = this.data.address;
          var sheng = address.sheng;
          var shi = address.shi;
          var qu = address.qu;
          var name = address.name;
          var phone = address.phone;
          address = address.address;
          var shopId = this.data.goodsInfo.shop_id;
          var data = {
              product:product,
              userid:userid,
              order_price:orderPrice,
              order_jf:orderJf,
              address:address,
              sheng:sheng,
              shi:shi,
              qu:qu,
              name:name,
              phone:phone,
              shop_id:shopId
          }
          // console.log(product,userid,orderJf,orderPrice,address);

          wx.showModal({
              title:'交易提示',
              content:'是否确认兑换该商品？',
              success(res){
                  if(res.confirm){
                      utils.postAjax(app.globalData.apiUrl+'/user_pay',data,function(res){
                          // res.data 2 积分不足   1成功  0失败
                          console.log(res);
                          if(res.data==1){
                              wx.showToast({
                                  title:'兑换成功！',
                                  icon:'none',
                                  duration:2000
                              })
                          }else if(res.data==2){
                              wx.showToast({
                                  title:'积分不足，兑换失败',
                                  icon:'none',
                                  duration:2000
                              })
                          }else if(res.data==0){
                              wx.showToast({
                                  title:'兑换失败，请重新兑换！',
                                  icon:'none',
                                  duration:1000
                              })
                          }
                      })
                  }else if(res.cancel){}
              }
          })
      }

    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this;
      wx.getStorage({
          key:'goodsInfo',
          success(res){
              console.log(res);
              that.setData({
                  goodsInfo:res.data,
                  totalPrice:res.data.price
              })
          }
      });
      utils.sendAjax(app.globalData.apiUrl+'/user_get_address','GET',function(res){
          var address = res.data
          for(var i=0;i<address.length;i++){
              if(address[i].status == 1){
                  that.setData({
                      addressId:address[i].id
                  })
                  wx.setStorageSync('addressId',address[i].id)
              }
          }
      },{userid:app.globalData.userId})
  },
    selectAddress(){
      wx.navigateTo({
          url:"../addressManager/index"
      })
    },
  //计算总价值
  calcTotalPrice(){
    var total = this.data.buyNum * this.data.goodsInfo.price;
    this.setData({
        totalPrice:total
    })
  },
  addNum(){
    let that = this;
    that.setData({
        buyNum:that.data.buyNum+1
    })
    this.calcTotalPrice();
  },
  reduceNum:function(){
    let that = this;
    if(that.data.buyNum == 1){
      wx.showToast({
          title: '购买数量不能为0',
          icon: 'none',
          duration: 2000
      })
    }else{
      that.setData({
          buyNum:that.data.buyNum-1
      })
        this.calcTotalPrice();
    }
  },
  toContactWay:function(){
    wx.navigateTo({
        url:"/subPages/shopPage/contactWay/contactWay"
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var addressId = '';
    setTimeout(function(){
        var addressId = wx.getStorageSync('addressId');
        console.log(addressId);
        utils.sendAjax(app.globalData.apiUrl+'/get_address_content','GET',function(res){
            console.log(res);
            that.setData({
                address:res.data
            })
        },{id:addressId})
    },200)

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
